package com.dome.user.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.dome.auth.utils.JwtUtils;
import com.dome.client.config.result.RtnResult;
import com.dome.client.config.result.RtnResultVO;
import com.dome.client.config.result.enums.RtnResultCodeEnums;
import com.dome.client.entity.vo.out.PageOut;
import com.dome.redis.utils.JedisUtils;
import com.dome.user.entity.bo.UserEntity;
import com.dome.user.entity.dto.UserLoginDTO;
import com.dome.user.entity.query.UserQuery;
import com.dome.user.entity.vo.UserLoginVO;
import com.dome.user.entity.vo.UserVO;
import com.dome.user.service.UserService;
import com.dome.user.transfer.UserTransfer;
import com.dome.util.annotation.NotRepeatSubmit;
import com.dome.util.constans.JWTConstants;
import com.dome.util.constans.RedisKeyConstants;
import com.dome.util.redis.RedisKeyUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Api(value = "用户接口", tags = "用户接口")
@RestController
public class UserController {

    @Autowired
    private UserService userService;
    @Value("${server.port}")
    private int port;


    @GetMapping("/test")
    public RtnResult test(@RequestParam Long id) {
        //JedisUtils.set("test", "hh", 60 * 30, TimeUnit.SECONDS);
        UserEntity userEntity=userService.test(id);
        return RtnResult.Success(port);
    }

    @NotRepeatSubmit
    @ApiOperation("幂等性提交方法")
    @GetMapping("/repeatSubmit")
    public RtnResult repeatSubmit(@RequestParam Long id) {
        //JedisUtils.set("test", "hh", 60 * 30, TimeUnit.SECONDS);
        UserEntity userEntity=userService.test(id);
        return RtnResult.Success(port);
    }


    @ApiOperation("自定义分页VO")
    @GetMapping("/page")
    public RtnResultVO<PageOut<List<UserVO>>> page() {
        UserQuery query=new UserQuery();
        query.setPageNum(1);
        query.setPageSize(2);
        PageOut<List<UserVO>> result = userService.getList(query);
        return RtnResultVO.Success(result);
    }

    @ApiOperation("plus自带分页")
    @GetMapping("/page-plus")
    public RtnResultVO<Page<List<UserVO>>> pagePlus() {
        UserQuery query=new UserQuery();
        query.setPageNum(1);
        query.setPageSize(2);
        Page<List<UserVO>> result = userService.pagePlus(query);
        return RtnResultVO.Success(result);
    }

    @RequestMapping(value = "/not-login")
    public RtnResult notLogin(){
        return RtnResult.Fail(RtnResultCodeEnums.NOTLOGIN);
    }

    @ApiOperation("模拟登入接口(弃用，从网关登入)")
    @GetMapping("/login")
    public RtnResult login(HttpServletRequest request){
        UserEntity userEntity=userService.selectById(1);
        //存redis
        UserLoginDTO userLoginDTO=UserTransfer.INSTANCE.toUserLoginDto(userEntity);
        String userKey = RedisKeyUtil.loginRedisKey(request, userEntity.getUserName());
        JedisUtils.setObject(userKey,userLoginDTO, RedisKeyConstants.COOKIE_TIMEOUT);

        //生成token
        Map<String,String> map=new HashMap<>();
        map.put(JWTConstants.ACCOUNT,userLoginDTO.getUserName());
        map.put(JWTConstants.SESSIONID,request.getSession().getId());
        String token=JwtUtils.genToken(map, JWTConstants.EXPIRESSECOND);
        //给前端展示的字段
        UserLoginVO userLoginVO=new UserLoginVO();
        userLoginVO.setUserName(userLoginDTO.getUserName());
        userLoginVO.setToken(token);
        return RtnResult.Success(userLoginVO);
    }
}
